package com.cal.utms.action.dealing;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.dealing.DealDao;
import com.cal.utms.dao.user_maintenance.UserDaoImpl;
import com.cal.utms.dto.dealing.RepoDealDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.User;
import com.cal.utms.model.dealing.RepoDealModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RepoDealAction extends ActionSupport implements ModelDriven<RepoDealDto> {

	private static final long serialVersionUID = 1L;
	private RepoDealDto repoDealDto = new RepoDealDto();

	@Override
	public RepoDealDto getModel() {
		return repoDealDto;
	}

	public String queryRepo() {
		DealDao dealDao = new DealDao();
		if (repoDealDto.getQueryOne() != null && !repoDealDto.getQueryOne().equals("")) {
			repoDealDto.setDeals(dealDao.queryDeals("REPO", repoDealDto.getQueryOne()));
		} else {
			repoDealDto.setDeals(dealDao.queryDeals("REPO"));
		}
		repoDealDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String queryDeal() {
		DealDao dealDao = new DealDao();
		RepoDealModel repoDealModel = new RepoDealModel();
		// dealDto.setDeal(dealDao.getById(dealDto.getId()));
		repoDealDto.setData(repoDealModel.mapRepoToDto(dealDao.getById(repoDealDto.getId())));
		repoDealDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String saveRepo() {
		RepoDealModel repoDealModel = new RepoDealModel();
		repoDealDto.setSuccess(true);

		Deal deal = repoDealModel.creatRepoDeal(repoDealDto);
		if (repoDealDto.isSuccess()) {
			DealDao dealDao = new DealDao();
			dealDao.addRepo(deal, repoDealDto);
		}
		repoDealDto.setDealCode(deal.getDealCode());
		return Action.SUCCESS;
	}

	public String modifyRepo() {
		if (repoDealDto.getDealCode() == null) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Empty Deal Code!");
			return Action.SUCCESS;
		}
		DealDao dealDao = new DealDao();
		Deal deal = dealDao.getByCode(repoDealDto.getDealCode());
		if (deal == null) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Invalid Deal Code!");
			return Action.SUCCESS;
		}
		RepoDealModel repoDealModel = new RepoDealModel();
		repoDealDto.setSuccess(true);
		deal = repoDealModel.updateRepoDeal(deal, repoDealDto);
		if (repoDealDto.isSuccess()) {
			dealDao.update(deal);
		}
		return Action.SUCCESS;
	}

	public String rejectRepoDeal() {
		if (repoDealDto.getDealCode() == null) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Empty Deal Code!");
			return Action.SUCCESS;
		}
		DealDao dealDao = new DealDao();
		Deal deal = dealDao.getByCode(repoDealDto.getDealCode());
		if (deal == null) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Invalid Deal Code!");
			return Action.SUCCESS;
		}
		deal.setStatus("Rejected");
		deal.setModifiedDate(new Date());
		deal.setModifiedUser(LoginAction.getCurrentLogingUser().getUserName());
		dealDao.update(deal);
		return Action.SUCCESS;
	}

	public String confirmRepo() {
		DealDao dealDao = new DealDao();
		repoDealDto.setSuccess(true);

		if (repoDealDto.getDealCode() == null) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Cannot Find Deal Code!");
			return Action.SUCCESS;
		}
		Deal oldDeal = dealDao.getByCode(repoDealDto.getDealCode());

		if (oldDeal.getCreatedUser().equals(LoginAction.getCurrentLogingUser().getUserName())) {
			repoDealDto.setSuccess(false);
			repoDealDto.setStatus("Created User cannot confirm!");
			return Action.SUCCESS;
		}

		RepoDealModel repoDealModel = new RepoDealModel();
		Deal deal = repoDealModel.confRepoDeal(repoDealDto);
		if (oldDeal.equals(deal)) {

			List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();
			Date today = new Date();
			MoneyTransaction creditEntry = new MoneyTransaction();
			MoneyTransaction debitEntry = new MoneyTransaction();
			User user = new UserDaoImpl().getUserByUsername(LoginAction.getCurrentLogingUser().getUserName());

			creditEntry.setCreditDebit("C");
			if (deal.getFund() != null) {
				creditEntry.setCustomerType("FUN");
				creditEntry.setTranTo(deal.getFund().getId());
			} else {
				repoDealDto.setStatus("Cannot Find Fund for Money Transactions!");
				repoDealDto.setSuccess(false);
				return Action.SUCCESS;
			}
			creditEntry.setDate(today);
			creditEntry.setEffectDate(deal.getValueDate());
			creditEntry.setFund(deal.getFund());
			creditEntry.setTranAmont(deal.getDealValue());
			creditEntry.setTranType("DEAL");
			creditEntry.setTranTypeId(deal.getId());
			creditEntry.setUser(user);

			debitEntry.setCreditDebit("D");
			if (deal.getIssuer() != null) {
				debitEntry.setCustomerType("ISU");
				debitEntry.setTranTo(deal.getIssuer().getId());
			} else {
				repoDealDto.setStatus("Cannot Find Issuer for Money Transactions!");
				repoDealDto.setSuccess(false);
				return Action.SUCCESS;
			}
			debitEntry.setDate(today);
			debitEntry.setEffectDate(deal.getValueDate());
			debitEntry.setFund(deal.getFund());
			debitEntry.setTranAmont(deal.getDealValue().multiply(new BigDecimal(-1)));
			debitEntry.setTranType("DEAL");
			debitEntry.setTranTypeId(deal.getId());
			creditEntry.setUser(user);

			moneyTransactions.add(creditEntry);
			moneyTransactions.add(debitEntry);

			/*
			 * if(deal.getWithholdingTax().equals("Upfront")){ creditEntry = new
			 * MoneyTransaction(); debitEntry = new MoneyTransaction();
			 * creditEntry.setCreditDebit("C");
			 * if(deal.getFund().getCustodian()!=null){
			 * creditEntry.setCustomerType("CTO");
			 * creditEntry.setTranTo(deal.getFund().getCustodian().getId());
			 * }else{
			 * repoDealDto.setStatus("Cannot Find Custodian for Money Transactions!"
			 * ); repoDealDto.setSuccess(false); return Action.SUCCESS; }
			 * creditEntry.setDate(today);
			 * creditEntry.setEffectDate(deal.getValueDate());
			 * creditEntry.setFund(deal.getFund());
			 * if(deal.getWithholdingTaxRate()==null){
			 * repoDealDto.setStatus("Cannot Find WHT Rate for Money Transactions!"
			 * ); repoDealDto.setSuccess(false); return Action.SUCCESS; }
			 * creditEntry
			 * .setTranAmont(deal.getDealValue().multiply(deal.getWithholdingTaxRate
			 * ())); creditEntry.setTranType("WHT");
			 * creditEntry.setTranTypeId(deal.getId());
			 * creditEntry.setUser(user);
			 * 
			 * debitEntry.setCreditDebit("D"); if(deal.getFund()!=null){
			 * debitEntry.setCustomerType("FUN");
			 * debitEntry.setTranTo(deal.getFund().getId()); }else{
			 * repoDealDto.setStatus
			 * ("Cannot Find Fund for Money Transactions!");
			 * repoDealDto.setSuccess(false); return Action.SUCCESS; }
			 * debitEntry.setDate(today);
			 * debitEntry.setEffectDate(deal.getValueDate());
			 * debitEntry.setFund(deal.getFund());
			 * debitEntry.setTranAmont(deal.getDealValue
			 * ().multiply(deal.getWithholdingTaxRate()).multiply(new
			 * BigDecimal(-1))); debitEntry.setTranType("WHT");
			 * debitEntry.setTranTypeId(deal.getId());
			 * creditEntry.setUser(user);
			 * 
			 * moneyTransactions.add(creditEntry);
			 * moneyTransactions.add(debitEntry); }
			 */

			dealDao.confirm(deal, moneyTransactions);
			repoDealDto.setStatus("OK");
			repoDealDto.setSuccess(true);
		} else {
			repoDealDto.setStatus("Deal Mismatch!");
			repoDealDto.setSuccess(false);
		}
		return Action.SUCCESS;
	}

	public String valuateDeal() {
		RepoDealModel repoDealModel = new RepoDealModel();
		repoDealDto.setSuccess(true);
		repoDealModel.valuate(repoDealDto);
		return Action.SUCCESS;
	}

}
